/**********************************************************
 * File description:addnewsample.cpp
 * Function: 新建样本库页面ui
 * Current Version: 1.0.0
 * Author: 张宇轩
 * Date: 2022-10-05
 *  -----
 *  Copyright by bokun Software.
 **********************************************************/
#include <string.h>
#include <QColorDialog>

#include "addnewsample.h"
#include "ui_addnewsample.h"

#include <QDebug>
#include <QMessageBox>

AddNewSample::AddNewSample(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddNewSample)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::CustomizeWindowHint|Qt::WindowCloseButtonHint);

    /* 限制文本框输入限制 */
    /* 样本名称 */
    QRegExp sampleNamerx("(\\w|-){1,40}");
    QRegExpValidator *sampleNameReg = new QRegExpValidator(sampleNamerx, this);
    ui->sampleName->setValidator(sampleNameReg);

    /* 初始化 */
    ui->sampleName->setPlaceholderText("请输入样本名");
    ui->comment->setPlaceholderText("样本备注信息");

    SLDB = new SampleLibrary();
    cusMsg = new CustomMessageBox();

    connect(ui->sampleColor, &QPushButton::clicked, this, &AddNewSample::getSampleColor);
    connect(ui->ok, &QPushButton::clicked, this, &AddNewSample::getNewSampleInfo);
    connect(ui->cancel, &QPushButton::clicked, this, [=](){
        ui->sampleName->clear();
        ui->comment->clear();
        ui->sampleColor->setStyleSheet("background: #000;");
        this->close();
    });

    /* 限制样本注释最大字符数 */
    connect(ui->comment, &QPlainTextEdit::textChanged, this, [=](){
        int maxLength = 30; // 最大注释限制字符数
        QString text = ui->comment->toPlainText();
        if(text.size() > maxLength){
            /* 获取当前光标 */
            QTextCursor cursor = ui->comment->textCursor();
            /* 获取当前光标位置 */
            int position = ui->comment->textCursor().position();
            /* 限制文本框值 */
            ui->comment->setPlainText(text.mid(0, maxLength));
            /* 设置文本框光标位置 */
            cursor.setPosition(maxLength, QTextCursor::MoveAnchor);
            ui->comment->setTextCursor(cursor);
        }
    });

}

AddNewSample::~AddNewSample()
{
    delete ui;
}

/* 获取样本 */
void AddNewSample::getDefaultSample(){
    /* 获取数据库中样本 */
    defaultHashList = SLDB->show();

    /* 获取默认样本 */
    QString path = QCoreApplication::applicationDirPath();
    char * filePath;
    QByteArray tempByte = (path + "/" + CONFIG_FILE_NAME).toLocal8Bit();
    filePath = tempByte.data();
    Simplexml * simplexml = new Simplexml(filePath);
    simplexml->next("reaction-plate");

    QList<Sample> defaultSamples; // 配置文件存储的样本数据格式
    QHash<QString, QString> defaultHash; // 渲染需要的的样本数据格式

    for(int i = 0;i < 3;i ++){
        Sample sample;
        sample.name = QString::fromStdString(simplexml->child("default-samples")->child("sample",i)->child("name")->text());
        defaultSamples.append(sample);
    }

    /* 将 QList<Sample> 改为 QList<QHash<QString, QString>> 后渲染 */
    for(int i = 0; i < defaultSamples.size(); i++){
        QString rgbNums = QString::number(rgbNum[12-i][0]) + "," + QString::number(rgbNum[12-i][1]) + "," + QString::number(rgbNum[12-i][2]);
        QString rgb = "rgb(" + rgbNums + ");";
        defaultHash.insert("color_value", rgb);
        defaultHash.insert("name", defaultSamples[i].name);
        defaultHash.insert("create_time", defaultSamples[i].create_time);
        defaultHash.insert("update_time", defaultSamples[i].update_time);
        defaultHash.insert("remarks",defaultSamples[i].remarks);
        defaultHash.insert("id", defaultSamples[i].id);
        defaultHashList.append(defaultHash);
    }
}


/*!
 * \brief 获取新建样本点颜色
 *
 * \return void
 *
 * Detail here!!!
 */
void AddNewSample::getSampleColor(){
    QColor c = QColorDialog::getColor(Qt::black, this);
    QString styleSheet = "background:";
    QString rgb = "rgb(";
    int r, g, b;
    r = c.red();
    g = c.green();
    b = c.blue();
    rgb += QString::number(r);
    rgb += ",";
    rgb += QString::number(g);
    rgb += ",";
    rgb += QString::number(b);
    rgb += ")";
    styleSheet += rgb;
    styleSheet += "; border: none;";
    ui->sampleColor->setStyleSheet(styleSheet);
    this->newSample.colorValue = rgb;
}

/*!
 * \brief 发送新样本信息
 *
 * \return void
 *
 * Detail here!!!
 */
void AddNewSample::getNewSampleInfo(){
    /* 判空 */
    if(ui->sampleName->text() == ""){
        cusMsg->show(this, CMsg::CWarning, "警告", "样本名不能为空！");
        return;
    };
    /* 获取样本库样本 */
    this->getDefaultSample();
    if(this->newSample.colorValue == "") this->newSample.colorValue = "rgb(0,0,0)";
    this->newSample.sampleName = ui->sampleName->text();
    this->newSample.comment = ui->comment->toPlainText();

    /* 判断是否重名 */
    for(int i = 0; i < defaultHashList.size(); i++){
        if(this->newSample.sampleName == defaultHashList[i]["name"]){
            cusMsg->show(this, CMsg::CInformation, "样本库", "样本名重复！");
            return;
        }
    }

    emit newSampleInfo(this->newSample);

    /* 初始化 */
    defaultHashList.clear();
    ui->sampleName->clear();
    ui->sampleColor->setStyleSheet("background: #000");
    ui->comment->clear();
}
